<template>
	<div class="review-content">
		<!-- 审核确认弹框 -->
		<AuditConfirm ref="auditDialog" :submit="handleAudit" />
		<div class="head">
			<span class="title">代理换线审核详情</span>
			<div v-if="showStatus(list)" class="right-btn">
				<el-button plain @click="goBack">
					{{ $t('common.cancel') }}
				</el-button>
				<el-button type="success" @click="() => $refs.auditDialog.successOpen()">
					{{ $t('agent.audit_through') }}
				</el-button>
				<el-button type="danger" @click="() => $refs.auditDialog.dangerOpen()">
					{{ $t('agent.audit_reject') }}
				</el-button>
			</div>
			<div v-else class="right-btn">
				<el-button plain @click="goBack">
					{{ $t('common.back') }}
				</el-button>
			</div>
		</div>
		<div class="main-content detailBoxStyle">
			<div class="review-content">
				<p class="name">实际被转代理信息</p>
				<div class="review-flex">
					<div>
						实际被转代理账号:
						{{ list.proxyName || '-' }}
					</div>
					<div>
						{{ $t('agent.agent_type') }}:
						{{ typeFilter(list.accountType, 'accountType') }}
					</div>
					<div>
						注册时间:
						{{list.registerDatetime || '-'}}
					</div>
					<div>
						风控层级: {{ list.windControlName || '-' }}
					</div>
					<div>
						{{ $t('common.belong_merchant') }}:
						{{ merchantFilter(list.merchantId) }}
					</div>
					<div>
						{{ $t('common.business_model') }}:
						{{ mappingBusinessModel(list.businessModel) || '-' }}
					</div>
					<div v-if="list.businessModel==2">
						信用额度处置:
						{{ list.creditQuotaType==1?'回收信用额度':'带走信用额度' }}
					</div>
				</div>
			</div>
			<div class="review-content">
				<p class="name">比例信息</p>
				<div class="table-flex">
					<AgentRebates
						ref="AgentRebates"
						labelWidth="160px"
						:isEdit="false"
						:queryRebateRateParams="queryRebateRateParams"
					/>
				</div>
			</div>
			<div class="review-content">
				<p class="name">{{ $t('agent.apply_info') }}</p>
				<div class="review-flex">
					<el-table
						border
						size="mini"
						:data="[1]"
						style="width: 100%"
						:header-cell-style="getRowClass"
						highlight-current-row
					>
						<el-table-column
							align="center"
							label="实际被转代理账号"
						>
							<template slot-scope="scope">
								{{ list.proxyName }}
							</template>
						</el-table-column>
						<el-table-column
							align="center"
							label="申请内容"
						>
							<template slot-scope="scope">
								{{ list.type === 1 ? '升级总代' : '-' }}
							</template>
						</el-table-column>
					</el-table>
				</div>
				<div class="review-flex">
					<div>
						{{ $t('agent.apply_people') }}:
						{{ list.applyName || '-' }}
					</div>
					<div>
						{{ $t('agent.apply_time') }}:
						{{ list.createdAt || '-' }}
					</div>
					<div class="show-remark">
						{{ $t('agent.apply_info') }}:
						{{ list.applyRemark || '-' }}
					</div>
				</div>
			</div>
			<div class="review-content">
				<p class="name">{{ $t('agent.audit_info') }}</p>
				<div class="review-flex">
					<div v-if="list.auditStatus === 1">
						{{ $t('agent.audit_people') }}: -
					</div>
					<div v-else>
						{{ $t('agent.audit_people') }}:
						{{ list.auditName || '-' }}
					</div>
					<div>
						{{ $t('agent.audit_time') }}:
						{{ list.auditDatetime || '-' }}
					</div>
					<div class="show-remark">
						{{ $t('agent.audit_remark') }}:
						{{ list.auditRemark || '-' }}
					</div>
				</div>
			</div>
		</div>
		<el-dialog
			:title="$t('agent.submit_confirm')"
			center
			:visible.sync="visible"
			:before-close="closeFormDialog"
			:width="getWidth('610px')"
			class="audit-confirm"
		>
			<el-form
				ref="form"
				:model="form"
				:rules="formRules"
				label-width="auto"
			>
				<el-form-item
					v-if="action"
					:label="$t('agent.submit_audit_info')"
				>
					<el-input
						v-model="form.remark"
						clearable
						type="textarea"
						:maxlength="500"
						show-word-limit
						:autosize="{ minRows: 4, maxRows: 4 }"
						style="width: 380px"
						:placeholder="$t('common.please_enter')"
					></el-input>
				</el-form-item>
				<el-form-item
					v-else
					:label="$t('agent.submit_audit_info')"
				>
					<el-input
						v-model="form.remark"
						clearable
						type="textarea"
						:maxlength="500"
						show-word-limit
						:autosize="{ minRows: 4, maxRows: 4 }"
						style="width: 380px"
						:placeholder="$t('common.please_enter')"
					></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="visible = false">
					{{ $t('agent.cancel') }}
				</el-button>
				<el-button type="primary" @click="auditOne">
					{{ $t('agent.submit') }}
				</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import list from '@/mixins/list'
import { getUsername, getUserInfo } from '@/utils/auth'
import AgentRebates from '@/components/AgentRebates'
import AuditConfirm from '@/components/AuditConfirm'

export default {
	components: {
		AgentRebates,
		AuditConfirm
	},
	mixins: [list],
	props: {
		// 审核 true 仅返回 false
		type: Boolean,
		rowData: {
			type: Object,
			default: () => {}
		},
		commissionContract: {
			type: Array,
			default: () => []
		},
		rebateContract: {
			type: Array,
			default: () => []
		}
	},
	data() {
		this.auditOne = this.throttle(this.auditOne, 1000)
		return {
			list: {},
			form: {
				remark: ''
			},
			name: '',
			userInfo: {},
			visible: false,
			action: false
		}
	},
	computed: {
		formRules() {
			return {
				remark: [
					{
						required: true,
						message: this.$t('agent.rule_audit_tip'),
						trigger: 'blur'
					}
				]
			}
		},
		queryRebateRateParams() {
			return {
				merchantId: this.rowData.merchantId,
				userId: this.rowData.proxyId,
				userName: this.rowData.userName,
				status: this.rowData.processStatus === 0 ? -1 : this.rowData.processStatus
			}
		}
	},
	watch: {
		rowData(val) {},
		$route(route) {
			if (route.name !== 'addReview') {
				this.goBack()
				this.$refs['AgentRebates'].resetFields()
			}
		}
	},
	created() {
		this.$emit('getPolicyType')
		this.getInfo()
		this.name = getUsername()
		this.userInfo = getUserInfo()
	},
	methods: {
		showStatus(row) {
			return (
				this.type &&
				(this.rowData.lockDatetime
					? +this.rowData.lockId === +this.userInfo.id
					: this.rowData.auditName === this.name)
			)
		},
		async queyrTemplateDetail() {
			try {
				const ret = await this.$api.getRebateRateTemplateDetail({
					userId: this.rowData.userId,
					userType: 1,
					queryType: 1,
					level: 1
				})
				if (ret.code === 200 && ret.data) {
					this.rebateRateTemplateName = ret.data[0].templateName
					this.rebateRateTemplateId = ret.data[0].templateId
				}
			} catch (err) {
				throw err
			}
		},
		closeFormDialog() {
			this.visible = false
		},
		confirm(action) {
			this.remark = ''
			this.form.remark = ''
			this.action = action
			this.visible = true
		},
		auditOne() {
			this.$refs.form.validate((valid) => {
				if (valid) {
					const loading = this.$loading({
						lock: true,
						text: 'Loading',
						spinner: 'el-icon-loading',
						background: 'rgba(0, 0, 0, 0.7)'
					})
					const params = {
						id: this.rowData.id,
						remark: this.form.remark,
						auditStatus: this.action ? 1 : 2
					}

					this.$api
						.upTopProxyAudit(params)
						.then((res) => {
							loading.close()
							if (res?.code === 200) {
								this.$message({
									type: 'success',
									message: this.$t(
										'common.action_success'
									)
								})
								this.visible = false
								this.goBack()
							} else {
								this.$message({
									message:
										res?.msg ||
										this.$t(
											'agent.interface_exceptions'
										),
									type: 'error'
								})
							}
						})
						.catch(() => {
							loading.close()
						})
				}
			})
		},
		handleAudit(form) {
			const loading = this.$loading({
				lock: true,
				text: 'Loading',
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.7)'
			})
			const params = {
				id: this.rowData.id,
				auditStatus: form.status,
				remark: form.remark
			}

			this.$api
				.upTopProxyAudit(params)
				.then((res) => {
					loading.close()
					if (res?.code === 200) {
						this.$message({
							type: 'success',
							message: this.$t(
								'common.action_success'
							)
						})
						this.visible = false
						this.goBack()
					} else {
						this.$message({
							message:
								res?.msg ||
								this.$t(
									'agent.interface_exceptions'
								),
							type: 'error'
						})
					}
				})
				.catch(() => {
					loading.close()
				})
		},
		goBack() {
			this.$emit('goBack')
		},
		getInfo() {
			const params = {
				id: this.rowData.id
			}
			this.$api.upTopProxyQueryAuditDetail(params).then((res) => {
				if (res?.code === 200) {
					this.loading = false
					this.list = res.data
				} else {
					this.loading = false
					this.$message({
						message:
							res?.msg || this.$t('agent.interface_exceptions'),
						type: 'error'
					})
				}
			})
		},
	}
}
</script>

<style lang="scss" scoped>
.table-flex {
	display: flex;
	width: 80%;
	margin-top: 15px;
}
::v-deep .el-dialog__header {
	text-align: left;
	color: #909399;
	font-weight: 700;
}
::v-deep .rempadding .el-dialog__body {
	padding: 15px 20px;
}
.review-content {
	width: 100%;
	.head {
		height: 70px;
		line-height: 70px;
		width: 100%;
		background: #000;
		border-radius: 2px;
		padding: 0 30px;
		.title {
			font-weight: 600;
			color: rgb(192, 190, 190);
		}
		.right-btn {
			float: right;
		}
	}
	.main-content {
		.review-content {
			min-height: 150px;
			border-top: 0px;
			padding: 30px;
			.name {
				font-weight: 600;
			}
			.review-flex {
				position: relative;
				width: 100%;
				margin-top: 15px;
				div {
					display: inline-block;
					width: 32%;
					line-height: 30px;
				}
				.show-remark {
					word-wrap: break-word;
					vertical-align: top;
				}
			}
		}
		.more-height {
			height: 200px;
		}
	}
}
</style>
